package com.shitou.util;

import java.util.ArrayList;
import java.util.List;

public class Page {
	private int totleSize;
	private int currentPage = 1;
	private int pageSize = 10;
	private int sliderSize = 10;
	
	/**
	 * 由page页对象返回显示开始到结束的集合
	 * @return
	 */
	public List<Long> getSlider() {

	    //sliderSize 是指显示页码的个数
	    int halfSize = this.sliderSize / 2;
	    long totalPage = getTotlePages();
	    //currentPage 当前的页码
	    long startPageNumber = Math.max(currentPage - halfSize, 1);
	    long endPageNumber = Math.min(startPageNumber + this.sliderSize - 1, totalPage);
	 
	    if (endPageNumber - startPageNumber < this.sliderSize) {
	        startPageNumber = Math.max(endPageNumber - this.sliderSize, 1);
	    }
	 
	    List<Long> result = new ArrayList<Long>();
	    for (long i = startPageNumber; i <= endPageNumber; i++) {
	        result.add(new Long(i));
	    }
	    return result;
	}
	
	public int getSliderSize() {
		return sliderSize;
	}

	public void setSliderSize(int sliderSize) {
		this.sliderSize = sliderSize;
	}

	public Page() {
	}
	
	public int getStartNum(){
		return pageSize * (currentPage - 1);
	}
	
	public int getTotlePages(){
		int result = totleSize / pageSize;
		if(totleSize == 0) {
			return 1;
		} else if(totleSize % pageSize == 0){
			return result;
		} else {
			return result + 1;
		}
	}
	public boolean isFirst(){
		return currentPage == 1;
	}
	public boolean isLast(){
		return currentPage == this.getTotlePages();
	}
	public boolean hasPrev(){
		return currentPage > 1;
	}
	public boolean hasNext(){
		return currentPage < this.getTotlePages();
	}
	
	public int getTotleSize() {
		return totleSize;
	}
	public void setTotleSize(int totleSize) {
		this.totleSize = totleSize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int page) {
		if(page > this.getTotlePages()){
			this.currentPage = this.getTotlePages();
		} else if(page < 1){
			this.currentPage = 1;
		} else {
			this.currentPage = page;
		}
			
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
}
